package arpui;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import arpui.bean.Device;
import arpui.util.Constants;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Separator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import jpcap.NetworkInterface;

/**
 * @author Guodeqi
 * 
 */
public class MainApplication extends Application {
	private Button startBtn = null;
	private Button stopBtn = null;
	private Button loginBtn = null;
	private Button configBtn = null;
	private Button configMultiBtn = null;
	private ProgressIndicator pin = null;
	private Label tmpInfoLabel = null;

	private TextField maxTime = null;

	private StackPane center = null;

	private TableView<Device> devTable = null;

	private ObservableList<Device> data = null;

	private AnchorPane glassPane = null;

	public NetworkInterface adapter = null;
	public byte[] netCardMac = null;
	public Stage mainStage = null;

	public void start(Stage stage) throws Exception {
		mainStage = stage;
		
		BorderPane root = new BorderPane();

		initUI(root);

		Scene scene = new Scene(root, 680, 600);
		stage.setScene(scene);

		addAction();

		Image icon = new Image(MainApplication.class.getResourceAsStream("bdcom32x32.gif"));
		stage.getIcons().add(icon);

		stage.setTitle(Constants.MAIN_TITLE);
		stage.setResizable(false);
		
		stage.show();
	}

	public void initUI(BorderPane root) {
		HBox top = new HBox();
		top.setPrefSize(680, 200);
		top.setSpacing(10);
		top.setPadding(new Insets(10));

		ListView<String> listAdapter = new ListView<>();
		ObservableList<String> items = FXCollections.observableArrayList(
				"Single", "Double", "Suite", "Family App", "Single", "Double",
				"Suite", "Family App", "Single", "Double", "Suite",
				"Family App", "Single", "Double", "Suite", "Family App");
		listAdapter.setItems(items);
		listAdapter.setPrefWidth(370);
		listAdapter.setMaxHeight(160);
		listAdapter.setMinHeight(160);

		VBox listAdapterBox = new VBox();
		listAdapterBox.setSpacing(7);
		listAdapterBox.getChildren().add(new Label("可用网卡:"));
		listAdapterBox.getChildren().add(listAdapter);

		AnchorPane oper = new AnchorPane();
		Label label = new Label("最大发现时间(s):");
		label.setFont(Font.font("", 16));
		maxTime = new TextField();
		maxTime.setPrefWidth(50);
		maxTime.setText("10");
		startBtn = new Button("开始发现");
		stopBtn = new Button("停止发现");

		HBox infoHbox = new HBox();
		infoHbox.setSpacing(10);
		infoHbox.getChildren().add(label);
		infoHbox.getChildren().add(maxTime);

		HBox btnHbox = new HBox();
		btnHbox.setSpacing(10);
		btnHbox.getChildren().add(startBtn);
		btnHbox.getChildren().add(stopBtn);

		oper.getChildren().add(infoHbox);
		oper.getChildren().add(btnHbox);

		oper.setBottomAnchor(infoHbox, 120D);
		oper.setBottomAnchor(btnHbox, 60D);
		oper.setLeftAnchor(infoHbox, 50D);
		oper.setLeftAnchor(btnHbox, 60D);

		Separator sepa = new Separator();
		sepa.setOrientation(Orientation.VERTICAL);

		top.getChildren().add(listAdapterBox);
		top.getChildren().add(sepa);
		top.getChildren().add(oper);
		/*------------------------------------------------*/
		center = new StackPane();

		devTable = new TableView<Device>();
		//表格列与model属性 绑定 
		TableColumn<Device, String> noCol = new TableColumn<Device, String>("No");
		noCol.setMaxWidth(60);
		noCol.setCellValueFactory(new PropertyValueFactory<Device, String>("no"));

		TableColumn IPCol = new TableColumn("IP Address");
		IPCol.setMinWidth(115);
		IPCol.setCellValueFactory(new PropertyValueFactory<Device, String>("ipAddress"));

		TableColumn maskCol = new TableColumn("IP Mask");
		maskCol.setMinWidth(115);
		maskCol.setCellValueFactory(new PropertyValueFactory<Device, String>("netMask"));

		TableColumn GWCol = new TableColumn("IP GateWay");
		GWCol.setMinWidth(115);
		GWCol.setCellValueFactory(new PropertyValueFactory<Device, String>("gateWay"));

		TableColumn macCol = new TableColumn("Mac Address");
		macCol.setMinWidth(115);
		macCol.setCellValueFactory(new PropertyValueFactory<Device, String>("macAddress"));

		TableColumn nameCol = new TableColumn("Device Name");
		nameCol.setMinWidth(115);
		nameCol.setCellValueFactory(new PropertyValueFactory<Device, String>("deviceName"));

		devTable.getColumns().addAll(noCol, IPCol, maskCol, GWCol, macCol,nameCol);

		data = FXCollections.observableArrayList();

		devTable.setItems(data);

		HBox buttomBtnBox = new HBox();
		buttomBtnBox.setAlignment(Pos.CENTER);
		buttomBtnBox.setSpacing(10);
		loginBtn = new Button("登录设备");
		configBtn = new Button("配置IP");
		configMultiBtn = new Button("配置多个设备");
		buttomBtnBox.getChildren().add(loginBtn);
		buttomBtnBox.getChildren().add(configBtn);
		buttomBtnBox.getChildren().add(configMultiBtn);
		VBox centerVbox = new VBox();
		Label tabellabel = new Label("已发现设备列表:");

		Separator sepa2 = new Separator();
		centerVbox.getChildren().add(sepa2);

		centerVbox.getChildren().add(tabellabel);
		centerVbox.getChildren().add(devTable);
		centerVbox.getChildren().add(buttomBtnBox);
		centerVbox.setSpacing(10);
		centerVbox.setPadding(new Insets(10));

		center.getChildren().add(centerVbox);
		root.setTop(top);
		root.setCenter(center);
	}

	public void addAction() {
		startBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				
				Platform.runLater(new Runnable() {
					public void run() {
						lockCenterPane();
					}
				});
				
				data.clear();
				initParam();

				String text = maxTime.getText();
				System.out.println("max time is :" + text);
				startTimer();
			}
		});

		stopBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				
				Platform.runLater(new Runnable() {
					public void run() {
						releaseCenterPane();
					}
				});
			}
		});

		loginBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				TableViewSelectionModel selectionModel = devTable
						.getSelectionModel();
				int selectedIndex = selectionModel.getSelectedIndex();
				if (selectedIndex == -1) {
					return;
				}
				Device device = data.get(selectedIndex);
				System.out.println(device);
			}
		});

		configBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				TableViewSelectionModel selectionModel = devTable
						.getSelectionModel();
				int selectedIndex = selectionModel.getSelectedIndex();
				if (selectedIndex == -1) {
					return;
				}
				Device device = data.get(selectedIndex);
				System.out.println(device);
			}
		});

		configMultiBtn.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				new ConfigMulti(mainStage);
			}
		});
	}

	public void lockCenterPane() {
		double x = ((center.getWidth() - 150) / 2);
		double y = ((center.getHeight() - 150) / 2);
		if (glassPane == null) {
			glassPane = new AnchorPane();
			pin = new ProgressIndicator();
			pin.setPrefSize(150, 150);

			glassPane.getChildren().add(pin);
			glassPane.setLeftAnchor(pin, x);
			glassPane.setTopAnchor(pin, y);

			tmpInfoLabel = new Label("正在发现...");
			tmpInfoLabel.setFont(Font.font("Cambria", 18));
			tmpInfoLabel.setTextFill(Color.web("#0076a3"));
			glassPane.setLeftAnchor(tmpInfoLabel, x + 30);
			glassPane.setTopAnchor(tmpInfoLabel, y + 68);

			glassPane.setStyle("-fx-background-color: #e0e8e2;-fx-opacity:0.6");
		}

		pin.setProgress(-1f);
		glassPane.getChildren().add(tmpInfoLabel);

		center.getChildren().add(glassPane);
	}

	public void releaseCenterPane() {
		center.getChildren().remove(glassPane);
	}

	public void startTimer() {

		Thread t = new Thread() {
			public void run() {
				try {
					for (int i = 0; i < 16; i++) {
						Device tmp = new Device((i + 1) + "", "192.168.1." + i,
								"255.255.255.0", "192.168.1.254",
								"FF FF FF FF FF F" + Integer.toHexString(i),
								"Switch" + i);
						Thread.sleep(100);
						data.add(tmp);
					}

					Platform.runLater(new Runnable() {
						public void run() {
							try {
								glassPane.getChildren().remove(tmpInfoLabel);
								pin.setProgress(1);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});

					Thread.sleep(500);

					Platform.runLater(new Runnable() {
						public void run() {
							try {
								center.getChildren().remove(glassPane);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
		t.setDaemon(true);
		t.start();
	}

	public void initParam() {

	}

	public static void main(String[] args) {
		launch(null);
	}

}
